<?php

namespace app\common\service\order;


use app\common\service\order\coupon_order\coupon\CouponOrderBuild;
use app\common\service\order\coupon_order\jutuike_channel\CouponOrderJutuikeBuild;
use app\common\service\order\pipeline\coupon\JutuikeAroundBeforeValidate;
use app\common\service\order\pipeline\coupon\JutuikeAroundValidate;
use app\common\service\order\pipeline\coupon\ProductInfo;
use app\common\service\order\pipeline\coupon\RequestValidate;
use app\common\service\order\pipeline\coupon\TestLimitMoney;
use app\common\service\order\pipeline\coupon\AccountValidate;
use app\common\service\pay\PayInterface;
use app\model\CouponOrder;
use app\model\CouponProductChannel;
use app\third\controller\Account;
use think\Exception;
use think\Pipeline;

class CouponOrderService
{

    /**
     * 创建订单的总入口
     */
    public function create($params)
    {

        $couponOrder = new CouponOrder();
        $couponOrder->params = $params;


        $pips = [
            [RequestValidate::class, 'handle'], //传参验证 AOP
            [AccountValidate::class, 'handle'], //如果是权益的，并且有账号充值规则的，去校验
            [ProductInfo::class, 'handle'], //商品信息处理  图片，商品名称
            [TestLimitMoney::class, 'handle'], //测试环境不能创建大于1元的产品

            [JutuikeAroundBeforeValidate::class, 'handle'], // 聚推客周边游 下单前 自己方的效验
            [JutuikeAroundValidate::class, 'handle'] // 聚推客周边游 第三方下单验证
        ];


        (new Pipeline())
            ->send($couponOrder)
            ->through($pips)
            ->then(function ($e) {
            });

        $servType = $couponOrder->serv_type ?? '';
        switch ($servType) {
            case PRODUCT_AROUND_SHOP_SERV: // 聚推客周边游
                $buildService = new CouponOrderJutuikeBuild($couponOrder);
                break;
            default:
                $buildService = new CouponOrderBuild($couponOrder);
                break;
        }

        $buildService->setParams($couponOrder->params);
        $couponOrder = $buildService
            ->saveOrder();

        if ($buildService->error) {
            throw new Exception($buildService->error);
        }

        return $couponOrder;

    }

    /**
     * 回调
     * @param PayInterface $pay
     * @return mixed
     */
    public function notify(PayInterface $pay)
    {
        return $pay->notify();
    }

    /**
     * @return CouponOrderBuild
     */
    public function calculateThePrice($couponProductChannel, $agent_id = 0, $premiumRate = [])
    {

        /**
         * @var CouponProductChannel $couponProductChannel
         */

        $couponOrder = new CouponOrder();
        $couponOrder->agent_id = $agent_id;

        $couponOrder->premiumRate = $premiumRate;

        if (is_string($couponProductChannel) || is_numeric($couponProductChannel)){

            $couponOrder->quote_id = $couponProductChannel;
            $buildService = new CouponOrderBuild($couponOrder);
        }else{
            $couponOrder->quote_id = $couponProductChannel->id;
            $buildService = new CouponOrderBuild($couponOrder);
            $buildService->setCouponProductChannel($couponProductChannel);
        }
        return $buildService;
    }


}